package
{
	import com.xcopy.Screen;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;

	public class Slot25FreeGameGirl extends Screen
	{
		public const TERMINATE:String            = 'TERMINATE';
		public const STATE_NONE:Number           = 0;
		public const STATE_WELCOME:Number        = 1;
		public const STATE_PICK:Number           = 2;
		public const STATE_COLLECT:Number        = 3;
		public const STATE_TAKE:Number           = 4;
		public const STATE_OVER:Number           = 5;
		public const STATE_AUTO:Number           = 6;
		public const TIME_MAX:Number             = 30;
		public const TIME_WELCOME:Number         = 2.0;
		public const TIME_FADE:Number            = 1.8;
		public const TIME_BREAK_AND_FLARE:Number = 0.4;
		public const TIME_BREAK_COLLECT:Number   = 0.8;
		public const TIME_BOUNTY_COLLECT:Number  = 0.3;
		public const TIME_END_COLLECT:Number     = 2.0;
		public const TIME_FLARE_5:Number         = 0.6;
		public const TIME_AUTO_OPEN:Number       = 0.6;
		public var reward:DigitalBox             = new DigitalBox();
		private var _state:Number                = STATE_NONE;
		private var _congratulation:MovieClip;
		private var _welcome:MovieClip;
		private var _info:MovieClip;
		private var _fade:MovieClip;
		private var _timeup:MovieClip;
		private var _heartLess:Number            = 0;
		private var _buttons:Array               = []; // 按鈕陣列
		private var _items:Array                 = []; // 道具陣列
		private var _hearts:Array                = []; // 心跳陣列
		private var _boxTime:DigitalBox          = new DigitalBox(this); // 倒數時間
		private var _boxTimes:DigitalBox         = new DigitalBox(this); // 獲得次數
		private var _boxRate:DigitalBox          = new DigitalBox(this); // 倍率
		private var _current:Number              = 0;
		private var _queue:Array                 = []; // 結果陣列
		private var _resultCurrent:Number        = 0; // 目前進行到的次數
		private var _resultBarNo:Array           = []; // 
		private var _resultLineCount:Array       = []; // 
		private var _resultLineSymbols:Array     = []; // 
		private var _resultWinPoint:Array        = []; // 
		private var _resultNowPoint:Array        = []; // 
		public var data:Object;
		private var _machine:Slot25Machine;

		public function get machine():Slot25Machine
		{
			return _machine;
		}

		public function get table():Slot25Table
		{
			return machine.table;
		}

		public function get debug():Slot25Debugger
		{
			return machine.debug;
		}

		public function Slot25FreeGameGirl(r:* = null)
		{
			super(r);
			_machine = r as Slot25Machine;
		}

		// 初始化物件
		override public function set clip(mc:MovieClip):void
		{
			super.clip = mc;
			_congratulation = clip.oCongratulation;
			_welcome = clip.oWelcome;
			_timeup = clip.oTimeup;
			_info = clip.oInfo;
			_fade = clip.oFade;
			_boxTime.clip = clip.oTime;
			_boxTimes.clip = _info.oTimes;
			_boxRate.clip = _info.oRate;
			reward.clip = clip.oReward;
			var mv:MovieClip;
			var bt:SimpleButton;
			var i:Number = 0;
			for (i = 0; i < 20; i++)
			{
				bt = clip.getChildByName('btnStock' + (i + 1)) as SimpleButton;
				setClickEvent(bt, createCallBack(onClick, i));
				_buttons[i] = bt;
				mv = clip.getChildByName('oItem' + (i + 1)) as MovieClip;
				_items[i] = mv;
			}
			for (i = 0; i < 3; i++)
			{
				mv = _info.getChildByName('oHeart' + (i + 1)) as MovieClip;
				_hearts[i] = mv;
			}
		}

		public function set state(n:Number):void
		{
			_state = n;
			var i:Number = 0;
			switch (_state)
			{
				case STATE_PICK:
					break;
				case STATE_COLLECT:
				case STATE_AUTO:
				case STATE_OVER:
					break;
			}
		}

		public function get state():Number
		{
			return _state;
		}

		public function set timeless(n:Number):void
		{
			_boxTime.number = n;
		}

		public function get timeless():Number
		{
			return _boxTime.number;
		}

		public function playSound(name:String):void
		{
			this.table.playSound(name);
		}

		// 初始化畫面，右方清空，button 清空，break 清空
		public function welcome():void
		{
			var i:Number = 0;
			var j:Number = 0;
			playSound('2000');
			state = STATE_WELCOME;
			timeless = TIME_MAX;
			_welcome.visible = true;
			_congratulation.visible = false;
			_timeup.visible = false;
			_info.visible = false;
			_fade.visible = false;
			_boxTime.disable();
			_boxTimes.number = 0;
			_boxRate.number = 1;
			_heartLess = 3;
			_resultCurrent = 0;
			_resultBarNo = data.BarNo;
			_resultLineCount = data.LineCount;
			_resultLineSymbols = data.LineSymbols;
			_resultWinPoint = data.WinPoint;
			_resultNowPoint = data.NowPoint;
			for (i = 0; i < 20; i++)
			{
				_queue[i] = {Face: data.Select[i]};
				_buttons[i].visible = false;
				_items[i].visible = false;
			}
			fadeEnable();
			addCaller(this, {time: TIME_WELCOME, count: 1, onUpdate: flip});
		}

		// 中央動畫開播（畫面翻轉效果）
		public function flip():void
		{
			this.table.music = 'Free';
			_welcome.visible = false;
			_fade.visible = true;
			_fade.gotoAndPlay('Start');
			addCaller(this, {time: TIME_FADE, count: 1, onUpdate: start});
		}

		public function start():void
		{
			var i:Number = 0;
			state = STATE_PICK;
			_boxTime.enable();
			_current = 0;
			_queue = [];
			_info.visible = true;
			for (i = 0; i < 20; i++)
			{
				_queue[i] = {Face: data.Select[i]};
				_buttons[i].visible = true;
				_buttons[i].enabled = true;
				_items[i].visible = false;
			}
			for (i = 0; i < 3; i++)
			{
				_hearts[i].visible = true;
			}
			_boxTimes.number = 0;
			_boxRate.number = 1;
			//trace('set time = ' + timeless);
			addTween(this, {timeless: 0, time: TIME_MAX, onComplete: timeup, transition: 'linear'});
		}

		// 時間到！開始自動開板
		public function timeup():void
		{
			state = STATE_AUTO;
			_timeup.visible = true;
			_timeup.gotoAndPlay('Start');
			_boxTime.disable();
			//trace('Time up');
			var i:Number = 0;
			for (i = 0; i < 20; i++)
			{
				_buttons[i].enabled = false;
			}
			addAutoOpen();
			//trace(data.SelectCount + ',' + data.PlayCount);
		/*

		var data:Object = {
		SelectCount : SelectCount, Select : Select, Rate : Rate, PlayCount : PlayCount,
		BarNo : BarNo, LineSymbols : LineSymbols, LineCount : LineCount, WinPoint : WinPoint, NowPoint : NowPoint
		};
		 * */
		}

		// 開啟一個石板
		public function doAutoOpen():void
		{
			if (state != STATE_AUTO)
			{
				return;
			}
			var i:Number     = 0;
			var index:Number = 0;
			for (i = 0; i < 20; i++)
			{
				if (_buttons[i].visible)
				{
					index = i;
					i = 20;
				}
			}
			onClick(index, false);
			addAutoOpen();
		}

		public function addAutoOpen():void
		{
			addCaller(this, {time: TIME_AUTO_OPEN, count: 1, onUpdate: doAutoOpen});
		}

		// 點選某個石板
		public function onClick(index:Number, byUser:Boolean = true):void
		{
			if ((state != STATE_PICK && state != STATE_AUTO) || (state == STATE_AUTO && byUser))
			{
				return;
			}
			playSound('1300');
			_buttons[index].visible = false;
			_items[index].visible = true;
			_items[index].f = _queue[_current].Face;
			_items[index].gotoAndPlay('Start');
			switch (_queue[_current].Face)
			{
				case 0:
					break;
				case 1:
				{
					_boxTimes.number += 1;
					addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: playSound, onUpdateParams: [1310]});
					break;
				}
				case 2:
				{
					_boxTimes.number += 2;
					addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: playSound, onUpdateParams: [1310]});
					break;
				}
				case 3:
				{
					_boxTimes.number += 3;
					addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: playSound, onUpdateParams: [1310]});
					break;
				}
				case 4:
				{
					_boxTimes.number += 5;
					addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: playSound, onUpdateParams: [1310]});
					break;
				}
				case 5:
				{
					_boxRate.number += 1;
					addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: playSound, onUpdateParams: [1320]});
					break;
				}
				case 7:
				{
					_heartLess -= 1;
					addCaller(this, {time: TIME_FLARE_5, count: 1, onUpdate: playSound, onUpdateParams: [1330]});
					_hearts[_heartLess].visible = false;
					break;
				}
			}
			_current++;
			//trace(_current + '/' + data.SelectCount);
			// 判斷是否已開到最後一個
			if (_current >= data.SelectCount)
			{
				//trace('開到尾巴了！！！');
				var i:Number = 0;
				removeTween(this, timeless);
				//addTween(this, {timeless : 0, time : 30, onComplete : timeup,
				//transition : 'linear'});
				for (i = 0; i < 20; i++)
				{
					_buttons[i].visible = false;
				}
				state = STATE_OVER;
				addCaller(this, {time: TIME_END_COLLECT, count: 1, onUpdate: prepareBar});
				//addCaller(this, {time : TIME_FLARE_5, count : 1, onUpdate :
				//flare});
				return;
			}
		}

		public function prepareBar():void
		{
			state = STATE_TAKE;
			var i:Number = 0;
			for (i = 0; i < 20; i++)
			{
				_buttons[i].visible = false;
				_items[i].visible = false;
			}
			_fade.visible = false;
			_boxTime.disable();
			_timeup.visible = false;
			this.machine.freeBarStart();
			addBar();
		}

		public function addBar():void
		{
			if (_boxTimes.number == 0)
			{
				addCaller(this, {time: TIME_END_COLLECT, count: 1, onUpdate: end});
				return;
			}
			var i:Number              = 0;
			var sendBarNo:Array       = [];
			var sendLineSymbols:Array = [];
			var sendLineCount:Array   = [];
			var sendWinPoint:Number   = 0;
			var sendNowPoint:Number   = 0;
			for (i = 0; i < 15; i++)
			{
				sendBarNo[i] = _resultBarNo[i + _resultCurrent * 15];
			}
			for (i = 0; i < 25; i++)
			{
				sendLineSymbols[i] = _resultLineSymbols[i + _resultCurrent * 25];
				sendLineCount[i] = _resultLineCount[i + _resultCurrent * 25];
			}
			sendWinPoint = _resultWinPoint[_resultCurrent];
			sendNowPoint = _resultNowPoint[_resultCurrent];
			this.debug.print('>> C2C_FreeGame');
			this.machine.freeBar();
			this.machine.isHolding = false;
			this.machine.onResult(sendBarNo, sendLineSymbols, sendLineCount, 5, sendWinPoint, sendNowPoint);
			_resultCurrent++;
			_boxTimes.number--;
		}

		public function end():void
		{
			this.table.music = '';
			playSound('2020');
			fadeDisable();
			//trace('Girl terminate');
			dispatchEvent(new Event(TERMINATE));
		}
	}
}
